package com.ydgg.ltar.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ydgg.ltar.mapper.ArBuildingContentMapper;
import com.ydgg.ltar.mapper.ArBuildingMapper;
import com.ydgg.ltar.model.ArBuilding;
import com.ydgg.ltar.model.ArBuildingContent;
import com.ydgg.ltar.util.PageInfo;
import com.ydgg.ltar.util.Result;


/**
 * 识楼内容
 * 作者： 张叶
 * 创建时间：2018年12月6日  下午4:39:59
 */
@Service
public class ArBuildingContentService {

	@Autowired
	private ArBuildingContentMapper arBuildingContentMapper;
	@Autowired
	private ArBuildingMapper arBuildingMapper;
	
	
	public ArBuildingContent selectByPrimaryKey(Long id){
		ArBuildingContent abc = arBuildingContentMapper.selectByPrimaryKey(id);
		return abc;
	}
	
	
	public List<ArBuildingContent> selectByWhere(String where){
		List<ArBuildingContent> list = arBuildingContentMapper.selectByWhere(where);
		return list;
	}  

	
	
	public List<Map<String,Object>> selectByPage(PageInfo pageInfo, String where, String order) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = arBuildingContentMapper.selectByPage(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}
	
	/**
	 * 添加
	 * 当type为3时，不限制个数
	 * 作者： 张叶
	 * 创建时间：2018年12月6日  下午4:42:58
	 */
	@Transactional
	public Result insert(ArBuildingContent abc){
		Result result = new Result();
		abc.setStatus("on");
		abc.setCreatedAt(new Date());
		Long abId = abc.getArBuildingId();
		ArBuilding ab = arBuildingMapper.selectByPrimaryKey(abId);
		if(ab != null){
			int i = arBuildingContentMapper.insert(abc);
			if(i > 0){
				result.code = 0;
				result.message = "添加成功";
			}else{
				result.code = 1;
				result.message = "系统错误，添加失败";
			}
		}else{
			result.code = 3;
			result.message = "所属大楼或已不存在，刷新重试";
		}
		return result;
	}
	
	
	/**
	 * 修改
	 * 作者： 张叶
	 * 创建时间：2018年12月29日  下午2:38:44
	 */
	public Result updateBySelective(ArBuildingContent abc){
		Result result = new Result();
		ArBuilding ar = arBuildingMapper.selectByPrimaryKey(abc.getArBuildingId());
		if(ar != null){
			int i = arBuildingContentMapper.updateBySelective(abc);	
			if( i > 0){
				result.code = 0;
				result.message = "修改成功";
			}else{
				result.code = 1;
				result.message = "系统错误，修改失败";
			}
		}else{
			result.code = 3;
			result.message = "此楼或已不存在，修改失败";
		}
		return result;
	}
//	public Result updateBySelective(ArBuildingContent abc){
//		Result result = new Result();
//		ArBuilding ar = arBuildingMapper.selectByPrimaryKey(abc.getArBuildingId());
//		if(ar != null){
//			int i = 0;
//			String type = ar.getType();
//			if(type.equals("3")){//不限栏目
//				i = arBuildingContentMapper.updateBySelective(abc);	
//			}else{
//				//根据修改ID，判断是否改变识楼
//				ArBuildingContent oldabc = arBuildingContentMapper.selectByPrimaryKey(abc.getId());
//				Long newAbId = abc.getArBuildingId();
//				Long oldAbId = oldabc.getArBuildingId();
//				if(newAbId != oldAbId){//改变楼
//					List<ArBuildingContent> list = arBuildingContentMapper.selectByWhere(" and ar_building_id = " + ar.getId());
//					if(list.size() < 4){
//						i = arBuildingContentMapper.updateBySelective(abc);	
//					}else{
//						result.code = 2;
//						result.message = "此楼分类已达上限，修改失败";
//					}
//				}else{
//					i = arBuildingContentMapper.updateBySelective(abc);	
//				}
//			}
//			if( i > 0){
//				result.code = 0;
//				result.message = "修改成功";
//			}else{
//				result.code = 1;
//				result.message = "系统错误，修改失败";
//			}
//		}else{
//			result.code = 3;
//			result.message = "此楼或已不存在，修改失败";
//		}
//		return result;
//	}
	
	
	public Result deleted(ArBuildingContent abc){
		Result result = new Result();
		//如有二级分类，阻止删除
		List<ArBuildingContent> list = arBuildingContentMapper.selectByWhere(" and status != 'deleted' and parent_id = " + abc.getId());
		if(list.size() == 0){
			abc.setStatus("deleted");
			int i = arBuildingContentMapper.updateBySelective(abc);
			if(i > 0){
				result.code = 0;
				result.message = "删除成功";
			}else{
				result.code = 1;
				result.message = "系统错误，删除失败";
			}
		}else{
			result.code = 2;
			result.message = "此分类有二级分类，无法删除";
		}
		return result;
	}
}
